package entity;

import java.io.Serializable;
import java.util.GregorianCalendar;
import java.util.Collection;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author Elise
 */

@Entity
@NamedQueries({
@NamedQuery(
    name = "findAllRosterEntriesForEmployee",
    query ="SELECT re FROM RosterEntry re WHERE :st MEMBER OF re.staffOnDuty"
    ),
@NamedQuery(
    name = "findAllRosterEntriesForWeek",
    query = "SELECT re " +
            "FROM RosterEntry re "+
            "WHERE re.week = :w"
    )
})       
public class RosterEntry implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;

    //Attributes////////////////////////////////////////
    private int shiftOfDay;
    private boolean pastShift;


    @Temporal(TemporalType.DATE)
    private GregorianCalendar startTime;

    @Temporal(TemporalType.DATE)
    private GregorianCalendar endTime;

    @Temporal(TemporalType.DATE)
    private GregorianCalendar ClockIn;

    @Temporal(TemporalType.DATE)
    private GregorianCalendar ClockOut;

    @OneToMany
    private List<Staff> staffOnDuty;

    private int week;
    //Constructors////////////////////////////////////
    public RosterEntry() {}


    public RosterEntry(int shift, GregorianCalendar start, GregorianCalendar end, List<Staff> staff){
        //Assumes scheduling, so no clock-ins exist.
        this.shiftOfDay = shift;
        this.startTime = start;
        this.endTime = end;
        this.staffOnDuty = staff;
        this.ClockIn = null;
        this.ClockOut = null;
        this.pastShift = false;
        week = startTime.get(startTime.WEEK_OF_YEAR);
    }


    //Access methods////////////////////////////////////
    public Long getId() {return id;}
    public void setId(Long id) {this.id = id;}

    public void setShift(int s){this.shiftOfDay = s;}
    public int getShift(){return shiftOfDay;}

    public void setStartTime(GregorianCalendar start){
        this.startTime = start;
        week = startTime.get(startTime.WEEK_OF_YEAR);
    }
    public GregorianCalendar getStartTime(){return startTime;}

    public int getDay(){return startTime.get(startTime.DATE);}
    public int getMonth(){return startTime.get(startTime.MONTH);}
    public int getYear(){return startTime.get(startTime.YEAR);}
    public int getSMinutes(){return endTime.get(startTime.MINUTE);}
    public int getSHours(){return startTime.get(startTime.HOUR_OF_DAY);}
    public int getEMinutes(){return endTime.get(startTime.MINUTE);}
    public int getEHours(){return startTime.get(startTime.HOUR_OF_DAY);}
    public int getWeek(){return week;}
    public void setEndTime(GregorianCalendar end){this.endTime = end;}
    public GregorianCalendar getEndTime(){return endTime;}

    //Read only - set by clockIn/clockOut
    public GregorianCalendar getClockIn(){return ClockIn;}
    public GregorianCalendar getClockOut(){return ClockOut;}

    public String getTime(){
        return startTime.get(startTime.HOUR_OF_DAY) + ":" +
                startTime.get(startTime.MINUTE);
    }

    public String getDate(){
        return startTime.get(startTime.DATE) + "/" +
                (startTime.get(startTime.MONTH)) + "/" +
                startTime.get(startTime.YEAR);
    }

    public void setStaff(List<Staff> st){
        staffOnDuty = st;
    }

    public Object getStaff(){
        String staffStr = "";
        if(staffOnDuty.isEmpty()) return staffStr;
        else{
            for(int i = 0;i < staffOnDuty.size(); i++){
                staffStr.concat(staffOnDuty.get(i).getName() + ", ");
            }

        return staffStr;
        }
    }
    //Other methods/////////////////////////////////
    public GregorianCalendar clockIn(){
        ClockIn = (GregorianCalendar) GregorianCalendar.getInstance();
        return ClockIn;
    }

    public GregorianCalendar clockOut(){
        ClockIn = (GregorianCalendar) GregorianCalendar.getInstance();
        pastShift = true;
        return ClockOut;
    }


    //Overrides////////////////////////////////////
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof RosterEntry)) {
            return false;
        }
        RosterEntry other = (RosterEntry) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return  "entity.RosterEntry[id=" + id + "]\n" +
                "entity.shiftOfDay[shiftOfDay=" + shiftOfDay + "]\n" +
                "entity.startTime[startTime=" + startTime.toString() + "]\n" +
                "entity.endTime[endTime=" + endTime.toString() + "]\n" +
                "entity.ClockIn[ClockIn=" + ClockIn.toString() + "]\n" +
                "entity.ClockOut[ClockOut=" + ClockOut.toString() + "]\n" +
                "entity.staffOnDuty[staffOnDuty=" + staffOnDuty.toString() + "]\n";
    }


}


